<template>
  <section class="page-content">
    <div class="toolbar">
      <el-form :inline="true" :model="params">
        <el-form-item label="来源列表">
          <el-select
            v-model.number="searchData.user_id"
            filterable
            remote
            clearable
            placeholder="输入用户账号进行检索"
            :remote-method="getUser"
            :loading="getUserLoading">
            <el-option
              v-for="item in users"
              :key="item.id"
              :label="item.account + '（'+ item.name +'）'"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData.data"
      @sort-change="sort"
      border
      style="width: 100%"
      max-height="100%">
      <el-table-column
        prop="user_id"
        sortable
        label="用户">
        <template slot-scope="scope">
          <span v-if="scope.row.user">
            {{scope.row.user.account}}({{scope.row.user.name}})
          </span>
        </template>
      </el-table-column>
     <el-table-column
        prop="amount"
        sortable
        label="金额">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 1">
            +
          </span>
          <span v-else>-</span>
          {{scope.row.amount}}
        </template>
      </el-table-column>
      <el-table-column
        prop="comment"
        label="描述">
      </el-table-column>
       <el-table-column
        prop="created_at"
        sortable
        label="时间">
      </el-table-column>
    </el-table>
    <div class="block toolbar">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.current_page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="parseInt(tableData.per_page)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
  </section>
</template>
<script>
  import {generateUrl} from '@/utils/helpers'
  export default {
    data () {
      return {
        loading: false,
        tableData: {
          first_page_url: '',
          from: '',
          to: '',
          last_page: '',
          last_page_url: '',
          next_page_url: '',
          prev_page_url: '',
          path: '',
          per_page: 0,
          total: 0,
          data: []
        },
        searchData: {
          user_id: null
        },
        params: {
          order_by: 'id',
          order_type: 'desc',
          page_size: 20,
          user_id: null,
          page: 1
        },
        users: [],
        getUserLoading: false
      }
    },
    mounted () {
      this._getData()
    },
    watch: {
      params: {
        deep: true,
        handler (newValue) {
          this._getData()
        }
      }
    },
    methods: {
      onSubmit () {
        this.params.user_id = this.searchData.user_id
      },
      handleSizeChange (val) {
        this.params.page_size = val
      },
      handleCurrentChange (val) {
        this.params.page = val
      },
      sort (data) {
        let orderType = ''
        if (data.order === 'ascending') {
          orderType = 'asc'
        }
        if (data.order === 'descending') {
          orderType = 'desc'
        }
        this.params.order_type = orderType
        this.params.order_by = data.prop
      },
      getUser (keywords) {
        if (keywords !== '') {
          let url = '/users?page_size=50&account=' + keywords
          this.getUserLoading = true
          this.$http.get(url).then((response) => {
            this.getUserLoading = false
            this.users = response.data.data
          }).catch((error) => {
            this.getUserLoading = false
            console.log(error)
          })
        }
      },
      _getData () {
        let url = generateUrl('/balances/record', this.params)
        this.loading = true
        this.$http.get(url).then((response) => {
          this.loading = false
          this.tableData = response.data
        }).catch((error) => {
          this.loading = false
          console.log(error)
        })
      }
    }
  }
</script>